package com.project.api.ym.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.project.api.common.ApiResponse;
import com.project.api.common.auth.LoginToken;
import com.project.api.ym.dao.ActivityDao;
import com.project.api.ym.dao.AdminDao;
import com.project.api.ym.model.Activity;
import com.project.api.ym.model.Admin;
import com.project.api.ym.service.ActivityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Null;
import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ActivityController {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private final String pf="ym";
    private final String vpf="verify/ym";

    @Autowired
    public ActivityService activityService;

    @RequestMapping(value=vpf+"/activity",method = RequestMethod.POST)
    @ResponseBody
    public String Activity(HttpServletRequest request, Model model,
                        @RequestBody JSONObject json){
        try {
            Object i=json.getOrDefault("id",0L);
            long activity_id=Long.valueOf(String.valueOf(i));
            Boolean isCreate= (Boolean) json.getOrDefault("isCreate",false);
            HttpSession session=request.getSession();
            long aid = Long.valueOf((String) session.getAttribute(LoginToken.ADMIN));
            if (activity_id>0L){
                activityService.UpdateActivity(activity_id,aid,json);
            }else{
                activity_id=activityService.CreateActivity(json,aid,aid);
            }
            JSONObject obj=new JSONObject();
            obj.put("activity_id",activity_id);
            return ApiResponse.Ok("操作成功",obj);
        } catch (Exception e) {
            return ApiResponse.Fail(e.getMessage());
        }
    }

    @RequestMapping(value=vpf+"/activity_list",method = RequestMethod.GET)
    @ResponseBody
    public String ActivityList(HttpServletRequest request, Model model,
                               @RequestParam(value="offset",required = false) @Nullable String  offset,
                               @RequestParam(value="limit",required = false) @Nullable String  limit,
                               @RequestParam(value="title",required = false) @Nullable String  title,
                               @RequestParam(value="atype",required = false) @Nullable String  atype,
                               @RequestParam(value="stime",required = false) @Nullable String  stime,
                               @RequestParam(value="etime",required = false) @Nullable String  etime,
                               @RequestParam(value="status",required = false) @Nullable String  status,
                               @RequestParam(value="sort",required = false) @Nullable String  sort,
                               @RequestParam(value="gtime",required = false) @Nullable String  gtime,
                               @RequestParam(value="otime",required = false) @Nullable String  otime,
                               @RequestParam(value="is_material",required = false) @Nullable String  is_material,
                               @RequestParam(value="is_hot",required = false) @Nullable String  is_hot,
                               @RequestParam(value="is_msg",required = false) @Nullable String  is_msg
                               ){
        Map<String , Object> map=new HashMap();
        int o=0;
        int l=20;
        if (offset!=null)o=Integer.parseInt(offset);
        if (limit!=null)l=Integer.parseInt(limit);
        if (title!=null&& !title.equalsIgnoreCase(""))map.put("title",title);
        if (atype!=null&&!atype.equalsIgnoreCase("-1"))map.put("atype",atype);
        if (stime!=null&&!stime.equalsIgnoreCase("-1"))map.put("stime",stime);
        if (etime!=null&&!etime.equalsIgnoreCase("-1"))map.put("etime",etime);
        if (gtime!=null&&!gtime.equalsIgnoreCase("-1"))map.put("gtime",gtime);
        if (otime!=null&&!otime.equalsIgnoreCase("-1"))map.put("otime",otime);
        if (is_material!=null&&!is_material.equalsIgnoreCase("-1"))map.put("is_material",is_material);
        if (is_hot!=null&&!is_hot.equalsIgnoreCase("-1"))map.put("is_hot",is_hot);
        if (is_msg!=null&&!is_msg.equalsIgnoreCase("-1"))map.put("is_msg",is_msg);
        if (status!=null&&!status.equalsIgnoreCase("-1"))map.put("status",status);
        List res=activityService.GetActivity(map,o,l,sort);
        int total=activityService.GetActivityCount(map);
        JSONObject json = new JSONObject();
        JSONArray jsonArray = new JSONArray(res);
        json.put("dataList",jsonArray);
        json.put("total",total);
        return ApiResponse.Ok("success",json);
    }

    @RequestMapping(value=vpf+"/activity_delete",method = RequestMethod.POST)
    @ResponseBody
    public String ActivityDelete(@RequestBody JSONObject json){
        try {
            ArrayList<String> ids = (ArrayList<String>) json.getOrDefault("ids",new ArrayList<>());
            activityService.Delete(ids);
            return ApiResponse.Ok("success", null);
        }catch (Exception e){
            return ApiResponse.Fail(e.getMessage());
        }
    }

    @RequestMapping(value=vpf+"/activity_update_status",method = RequestMethod.POST)
    @ResponseBody
    public String ActivityUpdateStatus(@RequestBody JSONObject json){
        try {
            Object i=json.getOrDefault("id",0L);
            long id=Long.valueOf(String.valueOf(i));
            Object s=json.getOrDefault("status",0L);
            int status=Integer.parseInt(String.valueOf(s));
            activityService.UpdateStatus(id, status);
            return ApiResponse.Ok("success", null);
        }catch (Exception e){
            return ApiResponse.Fail(e.getMessage());
        }
    }

    @RequestMapping(value=vpf+"/activity_update_send",method = RequestMethod.POST)
    @ResponseBody
    public String ActivityUpdateSend(@RequestBody JSONObject json){
        try {
            Object i=json.getOrDefault("id",0L);
            long id=Long.valueOf(String.valueOf(i));
            Object s=json.getOrDefault("is_send",0L);
            int send=Integer.parseInt(String.valueOf(s));
            activityService.UpdateSend(id, send);
            return ApiResponse.Ok("success", null);
        }catch (Exception e){
            return ApiResponse.Fail(e.getMessage());
        }
    }

    @RequestMapping(value=vpf+"/activity_one",method = RequestMethod.GET)
    @ResponseBody
    public String ActivityOne(@RequestParam(value="id",required = true)  String  id){
        try {
            long i = Long.valueOf(String.valueOf(id));
            Activity data = activityService.GetActivityById(i);
            return ApiResponse.Ok("success", data);
        }catch (Exception e){
            return ApiResponse.Fail(e.getMessage());
        }
    }
}
